define(['jquery', 'bootstrap', 'backend', 'table', 'form',], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotels/index',
                    add_url: 'hotels/add',
                    edit_url: 'hotels/edit',
                   // del_url: 'hotels/del',
                    multi_url: 'hotels/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'category',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk:'hid',
                sortName:'hid',
                columns: [
                    [
                        {checkbox: true},
                      //  {field: 'hid', title: __('Id'),operate: false},
                        {field: 'hoteltitle', title: __('h_title'), align: 'left'},
                        {field: 'h_img', title: __('Image'), operate: false, formatter: Table.api.formatter.image},//height:'50px'
                        {field: 'h_use', title: __('h_use'), align: 'left'},
                        {field: 'h_tel', title: __('h_tel'), align: 'left'},
                        {field: 'h_phone', title: __('h_phone'), align: 'left'},
                        {field: 'h_address', title: __('h_address'), align: 'left'},
                        {field: 'h_point', title: '评分', align: 'left'},
                        {field: 'h_status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'searchroom',
                                    text:'查看房间',
                                    title: __('查看房间'),
                                    classname: 'btn btn-xs btn-primary btn-view btn-addtabs',
                                    icon: 'fa fa-list',
                                    url: 'rooms?ref=addtabs',

                                },
                                {
                                    name: 'searchorder',
                                    text:'查看订单',
                                    title: __('查看订单'),
                                    classname: 'btn btn-xs btn-info btn-view btn-addtabs',
                                    icon: 'fa fa-list',
                                    url: 'order?ref=addtabs',

                                }
                            ]

                        }

                    ]
                ],
                commonSearch: false,

            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //修改添加是弹窗大小
            $(".btn-add").data("area", ["80%","80%"]); //自定义弹窗大小
            //修改编辑弹窗大小
            table.on('post-body.bs.table',function () {
                $(".btn-editone").data("area", ["80%","80%"]); //自定义弹窗大小
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },

        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);

            var citydid = 0;//默认省份下第一个城市id
            //重构
            $("#city").change(function () {
                $("#shiid").css("display",'block');
                var sel = $(this).val();
                $("#h_areadid").empty();
                //获取商圈
                $("#area").css("display",'block');
                $("#areasel").empty();

                $.ajax({
                    type: "GET",
                    url: "/api/task/getAreaDown",
                    data: {arreaid:sel},
                    dataType: "json",
                    success: function(data){
                        var len = data.length;
                        var html = "";
                        if(len > 0){
                            citydid = data[0]['areaId'];
                            for(var i=0;i<len;i++){
                                html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                            }
                        }
                        $("#h_areadid").append(html);
                        //获取商圈数据
                        $.ajax({
                            type: "GET",
                            url: "/api/task/getcityArea",
                            data: {shengid:sel,shiid:citydid},
                            dataType: "json",
                            success: function(data2){
                                var len2 = data2.length;
                                var html = "";
                                if(len2 > 0){
                                    for(var i=0;i<len2;i++){
                                        html += '<label class="checkbox-inline checkbox" pid="'+ data2[i]['cityid'] +'" style="line-height: 24px;clear: both;display: inline-block"><input type="checkbox" name="h_servs[]" checked value="'+ data2[i]['id'] +'">'+ data2[i]['atitle'] +'</label>';
                                    }
                                }
                                $("#areasel").append(html);
                            }
                        });

                    }
                });

            })



            //市切换h_areadid
            $("#h_areadid").change(function () {
                //获取省id
                var shengid = $("#city option:checked").val();
                //市id
                var citydid = $(this).val();
                $("#areasel").empty();
                //获取商圈数据
                $.ajax({
                    type: "GET",
                    url: "/api/task/getcityArea",
                    data: {shengid:shengid,shiid:citydid},
                    dataType: "json",
                    success: function(data2){
                        var len2 = data2.length;
                        var html = "";
                        if(len2 > 0){
                            for(var i=0;i<len2;i++){
                                html += '<label class="checkbox-inline checkbox" pid="'+ data2[i]['cityid'] +'" style="line-height: 24px;clear: both;display: inline-block"><input type="checkbox" name="h_servs[]" checked value="'+ data2[i]['id'] +'">'+ data2[i]['atitle'] +'</label>';
                            }
                        }
                        $("#areasel").append(html);
                    }
                });

                table.bootstrapTable('refresh', {});
            });

        },
        edit: function () {

            //选中省份
            var sel = $("#city").attr("ids");
            //选择市区
            var sel2 = $("#h_areadid").attr("ids");

            var sel3 = $("#h_servs").val();

            $("#city option[value="+ sel +"]").prop("selected",true);

            //获取对应省份市区
            var sel2 = $("#h_areadid").attr("ids");
            $("#h_areadid option[value="+ sel2 +"]").prop("selected",true);
            $.ajax({
                type: "GET",
                url: "/api/task/getAreaDown",
                data: {arreaid:sel},
                dataType: "json",
                success: function(data){
                    var len = data.length;
                    var html = "";
                    if(len > 0){
                        for(var i=0;i<len;i++){
                            html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                        }
                    }
                    $("#h_areadid").append(html);
                    $("#h_areadid option[value="+ sel2 +"]").prop("selected",true);
                }
            });

            //获取商圈数据
            $.ajax({
                type: "GET",
                url: "/api/task/getcityArea",
                data: {shengid:sel,shiid:sel2},
                dataType: "json",
                success: function(data2){
                    var len2 = data2.length;
                    var html = "";
                    if(len2 > 0){
                        for(var i=0;i<len2;i++){
                            html += '<label class="checkbox-inline checkbox" pid="'+ data2[i]['cityid'] +'" style="line-height: 24px;clear: both;display: inline-block"><input type="checkbox" name="h_servs[]" value="'+ data2[i]['id'] +'">'+ data2[i]['atitle'] +'</label>';
                        }
                    }
                    $("#areasel").append(html);
                    //选中商圈
                    if(sel3 != ""){
                        var areaarr = sel3.split(",");
                        for (i=0;i<areaarr.length ;i++ )
                        {
                            $("#areasel input[value="+  areaarr[i] +"]").prop("checked",true);
                        }
                    }
                }
            });



            var citydid = 0;//默认省份下第一个城市id
            //重构
            $("#city").change(function () {
                $("#shiid").css("display",'block');
                var sel = $(this).val();
                $("#h_areadid").empty();
                //获取商圈
                $("#area").css("display",'block');
                $("#areasel").empty();

                $.ajax({
                    type: "GET",
                    url: "/api/task/getAreaDown",
                    data: {arreaid:sel},
                    dataType: "json",
                    success: function(data){
                        var len = data.length;
                        var html = "";
                        if(len > 0){
                            citydid = data[0]['areaId'];
                            for(var i=0;i<len;i++){
                                html += ' <option pid="'+ data[i]['parentId'] +'" value="'+ data[i]['areaId'] +'">'+ data[i]['areaName'] +'</option>';
                            }
                        }
                        $("#h_areadid").append(html);
                        //获取商圈数据
                        $.ajax({
                            type: "GET",
                            url: "/api/task/getcityArea",
                            data: {shengid:sel,shiid:citydid},
                            dataType: "json",
                            success: function(data2){
                                var len2 = data2.length;
                                var html = "";
                                if(len2 > 0){
                                    for(var i=0;i<len2;i++){
                                        html += '<label class="checkbox-inline checkbox" pid="'+ data2[i]['cityid'] +'" style="line-height: 24px;clear: both;display: inline-block"><input type="checkbox" name="h_servs[]" checked value="'+ data2[i]['id'] +'">'+ data2[i]['atitle'] +'</label>';
                                    }
                                }
                                $("#areasel").append(html);
                            }
                        });

                    }
                });

            })

            //市切换h_areadid
            $("#h_areadid").change(function () {
                //获取省id
                var shengid = $("#city option:checked").val();
                //市id
                var citydid = $(this).val();
                $("#areasel").empty();
                //获取商圈数据
                $.ajax({
                    type: "GET",
                    url: "/api/task/getcityArea",
                    data: {shengid:shengid,shiid:citydid},
                    dataType: "json",
                    success: function(data2){
                        var len2 = data2.length;
                        var html = "";
                        if(len2 > 0){
                            for(var i=0;i<len2;i++){
                                html += '<label class="checkbox-inline checkbox" pid="'+ data2[i]['cityid'] +'" style="line-height: 24px;clear: both;display: inline-block"><input type="checkbox" name="h_servs[]" checked value="'+ data2[i]['id'] +'">'+ data2[i]['atitle'] +'</label>';
                            }
                        }
                        $("#areasel").append(html);
                    }
                });

                table.bootstrapTable('refresh', {});
            });








            //选中标签
            var hlebalsval = $("#h_lebals").val();
            var hlebalsvalarr= new Array(); //定义一数组
            hlebalsvalarr = hlebalsval.split(",");
            for (i=0;i<hlebalsvalarr.length ;i++ )
            {
                $("#h_lebalsval input[value="+ hlebalsvalarr[i] +"]").attr("checked","checked");
            }

            //选中房型
            var hhtypesval = $("#h_htypes").val();
            var hhtypesvalarr= new Array(); //定义一数组
            hhtypesvalarr = hhtypesval.split(",");
            for (i=0;i<hhtypesvalarr.length ;i++ )
            {
                $("#h_htypesval input[value="+ hhtypesvalarr[i] +"]").attr("checked","checked");
            }


            //选中房间属性
            var htypesval = $("#h_types").val();
            var htypesvalarr= new Array(); //定义一数组
            htypesvalarr = htypesval.split(",");
            for (i=0;i<htypesvalarr.length ;i++ )
            {
                $("#h_typesval input[value="+ htypesvalarr[i] +"]").attr("checked","checked");
            }


            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

        }
    };
    return Controller;
});
